package com.v1.video.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.CibnVideoDetailActivity;
import com.v1.video.domain.CibnVideoEntity;

import java.util.ArrayList;
import java.util.List;

public class CibnRecommendAdapter extends BaseAdapter {

	private Context mContext;
	private List<CibnVideoEntity> mLstData;
	private String mGenre;
	public CibnRecommendAdapter(Context context,String genre) {
		this.mContext = context;
		mLstData = new ArrayList<CibnVideoEntity>();
		mGenre = genre;
	}

	public List<CibnVideoEntity> getLstData() {
		return mLstData;
	}

	public void setLstData(List<CibnVideoEntity> lstData) {
		if (lstData != null && lstData.size() > 0) {
			this.mLstData = lstData;
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext,
					R.layout.activity_cibnvideodetail_recommend_item, null);
			holder = new Holder();
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.subtitle = (TextView) convertView
					.findViewById(R.id.tv_subtitle);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final CibnVideoEntity info = mLstData.get(position);
		if (!TextUtils.isEmpty(info.getVideoImage())) {
			ImageLoader.getInstance().displayImage(info.getVideoImage(),
					holder.image, Constant.VIDEO_10_16_OPTION_ROUNDED, null);
		} else {
			holder.image
					.setImageResource(R.drawable.common_place_holder_bg_16_10);
		}
		if (!TextUtils.isEmpty(info.getVideoName())) {
			holder.title.setText(info.getVideoName());
		} else {
			holder.title.setText("");
		}
		if (!TextUtils.isEmpty(info.getRecomm())) {
			holder.subtitle.setText(info.getRecomm());
		} else {
			holder.subtitle.setText("");
		}
		convertView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((CibnVideoDetailActivity)mContext).showRecommendVideo(info.getVideoId());
                
			}
		});
		return convertView;
	}
	
	private class Holder {
		public ImageView image;
		public TextView title;
		public TextView subtitle;
	}

}
